<!DOCTYPE html>
<html>
 <head> 
  <meta charset="UTF-8" /> 
  <title>首页</title> 
  <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.min.js"></script>
  <link rel="stylesheet" href="http://www.pintuer.com/css/pintuer.css">
  <script src="http://www.pintuer.com/js/pintuer.js"></script>
 </head> 
 <body> 
 <br>
   <br>
   登录用户信息：
   <ul>
     <li>用户名:<span id="loginName"></span></li>
   </ul>
   <br>
   
   模块列表：
   <table class="table table-bordered">
	<tr id="module_tit">
		<th>服务名称</th>
		<th>服务标识</th>
		<th>路由标识</th>
		<th>API文档</th>
		<th>启用</th>
	</tr>
</table>
   
 <br>
 调用demo模块服务：<br><br>
 &nbsp;<button id="createOrderBtn" onclick="createOrder();">生成订单</button><br><br>
 订单列表：
 <table class="table table-bordered">
	<tr id="order_tit">
		<th>订单号</th>
		<th>商品名称</th>
		<th>金额</th>
		<th>买家</th>
		<th>状态</th>
	</tr>
</table>
 </body>
 
 <script type="text/javascript">
 var user;
 $(document).ready(function() {
	 $.ajax({
         url: '/api/profile',
         type: "get",
         dataType: "json",
         async: false,
         success: function(json) {
        	 if(json.code == 200){
        		 user = json.data;
    			 $('#loginName').text(user.username); 
    			 loadModules();
    			 loadOrderList();
    		 }else if(json.code == 401){
    			 alert("未登录");
               	 setTimeout(function(){window.location.href = './login.html';},500);
    		 }else{
    			 alert(json.msg);
    		 }
         },
         error: function() {
        	 alert("error:未登录");
           	 setTimeout(function(){window.location.href = './login.html';},500);
         }
     });
	 
 });

 
 function loadModules(){
	 $.getJSON('/api/module/list',function(json){
		 if(json.code == 200){
			 var data = json.data;
			 var html = "";
			 for(var index in data){
				 html =  html + '<tr><td>'+data[index].name+'</td><td>'+data[index].serviceId+'</td><td>'+data[index].routeName+'</td><td><a href="'+data[index].apidocUrl+'" target="_blank">访问</a></td><td></td></tr>';
			 }
			 $(html).insertAfter('#module_tit');
		 }
	 });
 }
 
 function loadOrderList(){
	 var params = {};
	 params['pageNo'] = 1;
	 params['pageSize'] = 20;
	 $.ajax({
			dataType:"json",
		    type: "POST",
	        url: '/api/demo/order/list',
	        contentType: "application/json",
	        data:JSON.stringify(params) ,
			complete: function(){},
			success: function(json){
		        if(json.code==200){
		        	 var data = json.data.data;
		        	 var html = "";
					 for(var index in data){
						 html =  html + '<tr><td>'+data[index].orderNo+'</td><td>'+data[index].productName+'</td><td>'+data[index].amount+'</td><td><a href="/api/user/'+data[index].sellerId+'" target="_blank">'+data[index].sellerName+'</a></td><td>'+data[index].statusName+'</td></tr>';
					 }
					 $(html).insertAfter('#order_tit');
		          }else{
		        	 alert(json.msg);
		          }
		        },
			error: function(xhr, type){
				alert('系统错误');
			}
		});
 }
 
 function createOrder(){
	 var $btn = $('#createOrderBtn');
	 $btn.attr('disabled',true);
	 var params = {};
	 params['productId'] = 100001;
	 $.ajax({
			dataType:"json",
		    type: "POST",
	        url: "/api/demo/order/create",
	        contentType: "application/json",
	        data:JSON.stringify(params) ,
			complete: function(){$btn.removeAttr('disabled');},
			success: function(json){
				alert('下单成功');
				setTimeout(function(){window.location.reload();},500);
		    },
			error: function(xhr, type){
				alert('系统错误');
			}
		});
 }
 </script>
</html>